<?php

namespace app\common\model\mycurrency;

use app\common\model\MoneyLog;
use app\common\model\User;
use think\Model;


class UserMoneyLog extends Model
{

    // 表名
    protected $name = 'mycurrency_user_money_log';

    // 追加属性
    protected $append = [
        'type_text',
    ];

    const TYPE_HOUTAITIANJIA = 1;//余额变动类型 - 后台添加
    const TYPE_HOUTAIJIASHAO = 2;//余额变动类型 - 后台减少

    const TYPE_YONGHUCHONGZHI = 3;//余额变动类型 - 用户充值

    const TYPE_TIXIAN = 4;//余额变动类型 -用户发起提现
    const TYPE_TIXIANBOHUI = 5;//余额变动类型 - 用户提现驳回

    const TYPE_ZHIFUYAJIN = 6;//余额变动类型 = 支付押金
    const TYPE_TUIHUIYAJIN = 7;//余额变动类型 = 退回押金

    const TYPE_ZULINFEIYONGZHIFU = 8;//余额变动类型 = 支付租赁费用
    const TYPE_ZULINFEIYONGTUIHUEI = 9;//余额变动类型 = 租赁费用退回



    public static function type(){
        return [
            //加
            'plus' => [
                self::TYPE_HOUTAITIANJIA,
                self::TYPE_YONGHUCHONGZHI,
                self::TYPE_TIXIANBOHUI,
                self::TYPE_TUIHUIYAJIN,
                self::TYPE_ZULINFEIYONGTUIHUEI,
            ],
            //减
            'reduce' => [
                self::TYPE_HOUTAIJIASHAO,
                self::TYPE_TIXIAN,
                self::TYPE_ZHIFUYAJIN,
                self::TYPE_ZULINFEIYONGZHIFU,
            ],
        ];
    }

    /**
     * 处理余额变动
     * @param int $user_id 会员ID
     * @param int $money 变动金额
     * @param  int $type 变动类型
     * @param array $data 数据
     */
    public static function moneyChange($user_id,$money,$type,$data=[])
    {
        $user = User::get($user_id);
        $before = $user['money'];//变更前余额
        $after = 0;//变更后余额

        if(in_array($type,self::type()['plus'])){//后台添加  用户充值  用户提现驳回 用户下洗衣订单-差价退会
            $after = $before + $money;
        }elseif (in_array($type,self::type()['reduce'])){
            $after = $before - $money;
        }

        $user->money = $after;
        $user->save();

        $insertData = [
            'user_id' => $user['id'],
            'money' => $money,
            'before' => $before,
            'after' => $after,
            //'memo' => '',
            'createtime' => time(),
            'type' => $type
        ];
        $data = array_merge($insertData,$data);
        self::create($data);

        MoneyLog::moneyChange($user['id'],$money,$before,$after);
    }



    public function getCreatetimeAttr($value){
        return $value != null ? date('Y-m-d H:i:s', $value) : '';
    }

    public function getTypeList()
    {
        return [
            self::TYPE_HOUTAITIANJIA => __('后台添加'),
            self::TYPE_HOUTAIJIASHAO => __('后台减少'),
            self::TYPE_YONGHUCHONGZHI => __('用户充值'),
            self::TYPE_TIXIAN => __('用户提现'),
            self::TYPE_TIXIANBOHUI => __('用户提现驳回')
        ];
    }

    public function getTypeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['type']) ? $data['type'] : '');
        $list = $this->getTypeList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    public function user()
    {
        return $this->belongsTo('app\admin\model\User', 'user_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }


    public function order()
    {
        return $this->belongsTo('app\admin\model\mycurrency\UserRechargeOrder', 'recharge_order_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }


    public function withdrawal()
    {
        return $this->belongsTo('Withdrawal', 'money_withdrawal_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }
}
